package com.ecity.cn {
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Collision.Shapes.b2Shape;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2FixtureDef;
	import Box2D.Dynamics.b2World;
	import Box2D.Dynamics.Joints.b2RevoluteJointDef;
	import Box2D.Dynamics.Joints.b2WeldJointDef;
	import flash.geom.Point;
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	public class Door {
		private var _world:b2World;
		private const PIXELS_TO_METRE:int = 30;
		public function Door(world:b2World){
			_world = world;
		}
		
		private function addGate(position:Point,density:Number = 0, friction:Number = 0, restitution:Number = 0):b2Body {
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.type = b2Body.b2_dynamicBody;
			bodyDef.position.Set(p2m(position.x),p2m(position.y));
			var body:b2Body = this._world.CreateBody(bodyDef);////***************
			var gateShape:b2PolygonShape = new b2PolygonShape();
			var fixDef:b2FixtureDef = this.addFixtureDef(gateShape, density, friction, restitution);
			body.CreateFixture(fixDef);
			return body;
		}
		
		public function addJoint(anchor:b2Body,obj:b2Body):void {
			var jointer:b2RevoluteJointDef = new b2RevoluteJointDef();
			jointer.Initialize(anchor, obj, anchor.GetWorldCenter());
			jointer.maxMotorTorque = 1.0;  
			jointer.enableMotor = true;  
			_world.CreateJoint(jointer);
		}
		public function addWeldJoint(anchor:b2Body,body:b2Body):void {
			var joint:b2WeldJointDef = new b2WeldJointDef(); 
			joint.Initialize(anchor, body,anchor.GetWorldCenter());
			_world.CreateJoint(joint);
		}

		private function addAnchor(position:Point, radius:Number,density:Number = 0, friction:Number = 0, restitution:Number = 0):b2Body {
			var anchorDef:b2BodyDef = new b2BodyDef();
			anchorDef.type = b2Body.b2_staticBody;
			anchorDef.position.Set(p2m(position.x - 80), p2m(position.y));
			var anchor:b2Body = this._world.CreateBody(anchorDef);
			var ballshape:b2CircleShape = new b2CircleShape(p2m(5.0));
			var fixDef:b2FixtureDef = this.addFixtureDef(ballshape, density, friction, restitution);
			anchor.CreateFixture(fixDef);
			return anchor;
		}
		
		public function addFixtureDef(shape:b2Shape,density:Number = 0, friction:Number = 0, restitution:Number = 0):b2FixtureDef {
			var fixDef:b2FixtureDef = new b2FixtureDef();
			fixDef.shape = shape;
			fixDef.density = density;
			fixDef.friction = friction;
			fixDef.restitution = restitution;
			return fixDef;
		}
		
		private function p2m(pix:Number):Number {
			return pix / PIXELS_TO_METRE;
		}
		
	}

}